package com.xinqi.common.green.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.xinqi.common.green.enums.GreenScanLabelEnum;
import com.xinqi.common.green.enums.GreenScanSuggestionEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 阿里云 -- 内容审核 -- 公共方法、参数
 *
 * @Author：yyzhen
 * @Package：com.xinqi.common.base.utils.green
 * @Project：xinqiclass-cloud-plus
 * @name：SyncScan
 * @Date：2023/4/18 18:29
 * @Filename：SyncScan
 */
@Slf4j
public class SyncScan {

    public static final String accessKeyId = "accessKeyId";

    public static final String accessKeySecret = "accessKeySecret";

    public static final Integer connectTimeout = 3000;

    public static final Integer readTimeout = 10000;

    /**
     * 业务场景
     */
    public static final String BIZ_TYPE = "xinqiclass_biz";

    /**
     * 获取阿里云内容安全校验配置信息异常
     */
    public static IClientProfile getProfile() {
        /*
         * cn-shanghai：华东2（上海）
         * cn-beijing：华北2（北京）
         * cn-shenzhen：华南1（深圳）
         * ap-southeast-1：新加坡
         */
        IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        } catch (Exception e) {
            log.error("获取阿里云内容安全校验配置信息异常，请检查配置信息", e);
        }
        return profile;
    }


    /**
     * 文本 -- 内容安全审核结果处理
     */
    public static boolean getTextListResult(List<Map<String, String>> mapList) {
        boolean result = Boolean.TRUE;
        if (CollectionUtil.isEmpty(mapList)) {
            return result;
        }
        for (Map<String, String> item : mapList) {
            String suggestion = item.get("suggestion");
            if (Objects.equals(GreenScanSuggestionEnum.BLOCK.getCode(), suggestion)) {
                String label = item.get("label");
                if (Objects.equals(GreenScanLabelEnum.ABUSE.getCode(), label)) {
                    //辱骂
                    result = Boolean.FALSE;
                } else if (Objects.equals(GreenScanLabelEnum.PORN.getCode(), label)) {
                    //色情
                    result = Boolean.FALSE;
                } else if (Objects.equals(GreenScanLabelEnum.CONTRABAND.getCode(), label)) {
                    //违禁
                    result = Boolean.FALSE;
                } else if (Objects.equals(GreenScanLabelEnum.TERRORISM.getCode(), label)) {
                    //暴恐
                    result = Boolean.FALSE;
                } else if (Objects.equals(GreenScanLabelEnum.POLITICS.getCode(), label)) {
                    //涉政
                    return Boolean.FALSE;
                } else if (Objects.equals(GreenScanLabelEnum.CUSTOMIZED.getCode(), label)) {
                    //自定义
                    result = Boolean.FALSE;
                }
            }
        }

        return result;
    }

    /**
     * 文本 -- 内容安全审核结果处理
     */
    public static boolean getTextResult(Map<String, String> map) {
        if (CollectionUtil.isEmpty(map)) {
            return Boolean.TRUE;
        }
        String suggestion = map.get("suggestion");
        if (Objects.equals(GreenScanSuggestionEnum.BLOCK.getCode(), suggestion)) {
            String label = map.get("label");
            if (Objects.equals(GreenScanLabelEnum.ABUSE.getCode(), label)) {
                //辱骂
                return Boolean.FALSE;
            } else if (Objects.equals(GreenScanLabelEnum.PORN.getCode(), label)) {
                //色情
                return Boolean.FALSE;
            } else if (Objects.equals(GreenScanLabelEnum.CONTRABAND.getCode(), label)) {
                //违禁
                return Boolean.FALSE;
            } else if (Objects.equals(GreenScanLabelEnum.TERRORISM.getCode(), label)) {
                //暴恐
                return Boolean.FALSE;
            } else if (Objects.equals(GreenScanLabelEnum.POLITICS.getCode(), label)) {
                //涉政
                return Boolean.FALSE;
            } else if (Objects.equals(GreenScanLabelEnum.CUSTOMIZED.getCode(), label)) {
                //自定义
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }


}
